const _ = require('lodash');
const sysuserFacade = require('./../facade/sysuser.server.facade.js');
module.exports = {
    addSysUser : addSysUser,
    getSysUser : getSysUser,
    login : login,
    logout : logout,
    isLogin : isLogin
}

function addSysUser(req,res,next){
    let name = req.body.name;
    let password = req.body.password;

    sysuserFacade.addSysUser(name,password, (error,result)=>{
        if(error) return next(error,req,res);
        return res.json(result);   
    });
}

function getSysUser(req,res,next){
    let name = req.body.name;
    let password = req.body.password;

    sysuserFacade.getSysUser(name,password, (error,result)=>{
        if(error) return next(error,req,res);
        return res.json(result);   
    });
}

function login(req,res,next){
    let name = req.body.name;
    let password = req.body.password;

    sysuserFacade.login(name,password, (error,result)=>{
        if(error) return next(error,req,res);
        if(result){
            req.session.sysuser = name;
        }
        return res.json(result);   
    });
}

function isLogin(req,res,next){
    return res.json((req.session.sysuser) ? true : false);
}

function logout(req,res,next){
    delete req.session.sysuser;
    return res.json(true);
}